package com.fetaphon.blelibrary.utils;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class ByteUtils {

    /**
     * int转byte[] 高位在前
     *
     * @param value
     * @param length
     * @return
     */
    public static byte[] int2Bytes(int value, int length) {
        byte[] bytes = new byte[length];
        while (length > 0) {
            length--;
            bytes[length] = (byte) (value >> 8 * length & 0xFF);
        }
        return bytes;
    }

    public static int byteToInt(byte[] bytes) {
        int number = 0;
        for (int i = 0; i < bytes.length; i++) {
            number += (bytes[i] & 0xFF) << i * 8;
        }
        return number;
    }

    /**
     * byte转int 有符号
     *
     * @param bytes
     * @return
     */
    public static int byteToIntSign(byte[] bytes) {
        int number = 0;
        for (int i = 0; i < bytes.length; i++) {
            number += bytes[i] << i * 8;
        }
        return number;
    }

    public static byte[] long2Bytes(long value, int length) {
        byte[] bytes = new byte[length];
        while (length > 0) {
            length--;
            bytes[length] = (byte) (value >> 8 * length & 0xFF);
        }
        return bytes;
    }

    public static long byteToLong(byte[] bytes) {
        long number = 0;
        for (int i = 0; i < bytes.length; i++) {
            number += (bytes[i] & (long) 0xff) << i * 8;
        }
        return number;
    }

    public static long byteToLongSign(byte[] bytes) {
        long number = 0;
        for (int i = 0; i < bytes.length; i++) {
            number += (bytes[i]) << i * 8;
        }
        return number;
    }

    public static byte[] float2Bytes(float value, int length) {
        int fbit = Float.floatToIntBits(value);
        byte[] bytes = new byte[length];
        while (length > 0) {
            length--;
            bytes[length] = (byte) (fbit >> 8 * length & 0xFF);
        }
        return bytes;
    }

    /**
     * int转byte[] 高位在后
     *
     * @param value
     * @param length
     * @return
     */
    public static byte[] int2Bytes1(int value, int length) {
        byte[] bytes = new byte[length];
        while (length > 0) {
            length--;
            bytes[length] = (byte) (value >> 8 * (bytes.length - length - 1) & 0xFF);
        }
        return bytes;
    }

    public static byte[] byteMerge(byte[]... values) {
        int length_byte = 0;
        for (int i = 0; i < values.length; i++) {
            length_byte += values[i].length;
        }
        byte[] all_byte = new byte[length_byte];
        int countLength = 0;
        for (int i = 0; i < values.length; i++) {
            byte[] b = values[i];
            System.arraycopy(b, 0, all_byte, countLength, b.length);
            countLength += b.length;
        }
        return all_byte;
    }

    /**
     * @param list
     * @return
     */
    public static byte[] toArray(List<Integer> list) {
        if (list == null || list.size() < 0)
            return null;
        byte[] bytes = new byte[list.size()];
        int i = 0;
        Iterator<Integer> iterator = list.iterator();
        while (iterator.hasNext()) {
            bytes[i] = new Integer(iterator.next() & 0xff).byteValue();
            i++;
        }
        return bytes;
    }


    public static byte[] byteToBit(byte a) {
        byte[] temp = new byte[8];
        for (int i = 7; i >= 0; i--) {
            temp[i] = (byte) ((a >> i) & 1);
        }
        return temp;
    }

    public static byte bitToByte(String bit) {
        int re, len;
        if (null == bit) {
            return 0;
        }
        len = bit.length();
        if (len != 4 && len != 8) {
            return 0;
        }
        if (len == 8) {// 8 bit处理
            if (bit.charAt(0) == '0') {// 正数
                re = Integer.parseInt(bit, 2);
            } else {// 负数
                re = Integer.parseInt(bit, 2) - 256;
            }
        } else {//4 bit处理
            re = Integer.parseInt(bit, 2);
        }
        return (byte) re;
    }

    public static String toHexString(byte[] byteArray) {
        if (byteArray == null || byteArray.length < 1)
            throw new IllegalArgumentException("this byteArray must not be null or empty");
        final StringBuilder hexString = new StringBuilder();
        for (int i = 0; i < byteArray.length; i++) {
            if ((byteArray[i] & 0xff) < 0x10)//0~F前面补零
                hexString.append("0");
            hexString.append(Integer.toHexString(0xFF & byteArray[i]));
        }
        return hexString.toString().toLowerCase();
    }

    public static String toString(byte[] byteArray) {
        try {
            return new String(byteArray, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static byte[] subBytes(byte[] src, int start, int len) {
        byte[] bs = new byte[len];
        for (int i = start; i < start + len; i++) bs[i - start] = src[i];
        return bs;
    }

    public static byte[][] splitBytes(byte[] bytes, int size) {
        double splitLength = Double.parseDouble(size + "");
        int arrayLength = (int) Math.ceil(bytes.length / splitLength);
        byte[][] result = new byte[arrayLength][];
        int from, to;
        for (int i = 0; i < arrayLength; i++) {
            from = (int) (i * splitLength);
            to = (int) (from + splitLength);
            if (to > bytes.length)
                to = bytes.length;
            result[i] = Arrays.copyOfRange(bytes, from, to);
        }
        return result;
    }

}